<?php

/**
 * Klasa do wysyłania poczty - konfigurację czyta z naszej konfiguracje oraz korzysta z templatów
 *
 */
class Core_Mail extends Zend_Mail
{
	/**
	 * Zend_View
	 */
	protected $_view;
	
	/**
	 * Script path
	 * 
	 * @var string;
	 */
	protected static $_scriptpath;
	/**
	 * Domyślny mail nadawcy
	 * 
	 * @var string;
	 */
	protected static $_mail;
	/**
	 * Domyślna nazwa nadawcy
	 * 
	 * @var string;
	 */
	protected static $_name;
	
	/**
	 * Konstruktor
	 *
	 * @param string $charset Kodowanie
	 */
	public function __construct($charset = 'utf-8', $scriptPath = null)
	{
		// build a view
		$this->_view = new Zend_View;
		if (is_null($scriptPath))
			$scriptPath = self::$_scriptpath;
		$this->_view->setScriptPath($scriptPath);
		$this->_charset = $charset;
	}
	
	/**
	 * Sets Default From-email and name of the message
	 *
	 * @param  string               $email
	 * @param  string    Optional   $name
	 * @return void
	 */
	public static function setDefaultFrom($email, $name = null)
	{
		self::$_mail = $email;
		self::$_name = $name;
	}
	
	/**
	 * Ustawia domyślną ścieżkę do skryptu do renderowania maili
	 *
	 * @param string $scriptPath Ścieżka
	 */
	public static function setDefaultScriptPath($scriptPath)
	{
		self::$_scriptpath = $scriptPath;
	}
	
	/**
	 * Zwraca obiekt widoku
	 *
	 * @return Zend_View
	 */
	public function getView()
	{
		return $this->_view;
	}
	
	/**
	 * Sets the HTML body for the message frpm template (script)
	 *
	 * @param  string    $template
	 * @param  array     $arguments
	 * @param  string    $charset
	 * @param  string    $encoding
	 * @return Zend_Mail Provides fluent interface
	 */
	public function setBodyTemplate($template, $arguments = array(), $charset = null, $encoding = Zend_Mime::ENCODING_8BIT)
	{
		if (empty($template))
			throw new Zend_Mail_Exception('Script name is empty');
		if (empty($charset))
			$charset = $this->_charset;
		
		// Assign each item
		if (!empty($arguments))
			foreach($arguments as $k => $v){
				$this->_view->assign($k, $v);
			}
		
		// render the template
		$body = $this->_view->render($template . '.phtml');
		
		$this->setBodyHtml($body, $charset, $encoding);
		
		return $this;
	}
	
	/**
	 * Sets the HTML body for the message
	 *
	 * @param  string    $html
	 * @param  string    $charset
	 * @param  string    $encoding
	 * @return Zend_Mail Provides fluent interface
	 */
	public function setBodyHtml($html, $charset = null, $encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE)
	{
		$this->setBodyText(strip_tags($html), $charset, $encoding);
		return parent::setBodyHtml($html, $charset, $encoding);
	}
	
	/**
	 * Sends this email using the given transport or a previously
	 * set DefaultTransport or the internal mail function if no
	 * default transport had been set.
	 *
	 * @param  Zend_Mail_Transport_Abstract $transport
	 * @return Zend_Mail Provides fluent interface
	 */
	public function send($transport = null)
	{
		if (null == $this->getFrom()) {
			$this->setFrom(self::$_mail, self::$_name);
		}
		
		try {
			$return = parent::send($transport);
			Zend_Registry::get('Zend_Log')->notice('Mail has sent to ' . implode(', ', $this->getRecipients()));
		} catch (Exception $e) {
			Zend_Registry::get('Zend_Log')->err('Error sending mail ' . $e->getMessage());
			throw new Zend_Mail_Exception($e->getMessage());
		}
		return $this;
	}
}